//
//  LaunchAdManager.m
//  YiLin
//
//  Created by Zyark on 2018/7/17.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "LaunchAdManager.h"
#import "ZYIndexDataViewModel.h"
#import "XHLaunchAd.h"

@interface LaunchAdManager()<XHLaunchAdDelegate>
@property(nonatomic,strong) ZYIndexDataViewModel *viewModel;
@end

@implementation LaunchAdManager
//+(void)load{
//    [self shareManager];
//}
//
//+(LaunchAdManager *)shareManager{
//    static LaunchAdManager *instance = nil;
//    static dispatch_once_t oneToken;
//    dispatch_once(&oneToken,^{
//        instance = [[LaunchAdManager alloc] init];
//    });
//    return instance;
//}

- (instancetype)init{
    self = [super init];
    if (self) {
        //在UIApplicationDidFinishLaunching时初始化开屏广告,做到对业务层无干扰,当然你也可以直接在AppDelegate didFinishLaunchingWithOptions方法中初始化
        //        if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {
        [self bindView];
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            //初始化开屏广告
            [self.viewModel.adCommand execute:nil];
        }];
        //        }
    }
    return self;
}

-(void)bindView{
    @weakify(self);
    [[[self.viewModel.adCommand executionSignals] switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        //配置广告数据
        XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
        //广告停留时间
        imageAdconfiguration.duration = 3;
        //广告frame
        imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
        imageAdconfiguration.imageNameOrURLString = x;
        //设置GIF动图是否只循环播放一次(仅对动图设置有效)
        imageAdconfiguration.GIFImageCycleOnce = NO;
        //缓存机制(仅对网络图片有效)
        //为告展示效果更好,可设置为XHLaunchAdImageCacheInBackground,先缓存,下次显示
        imageAdconfiguration.imageOption = XHLaunchAdImageDefault;
        //图片填充模式
        imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;
        //广告显示完成动画
        imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
        //广告显示完成动画时间
        imageAdconfiguration.showFinishAnimateTime = 0.8;
        //跳过按钮类型
        imageAdconfiguration.skipButtonType = SkipTypeRoundProgressTime;
        //        XHLaunchAdButton *button = [[XHLaunchAdButton alloc] init];
        //        button.frame.size = CGSizeMake(, <#CGFloat height#>)
        //        imageAdconfiguration.customSkipView = button];
        
        //        imageAdconfiguration.skipButtonType = SkipTypeNone;
        //后台返回时,是否显示广告
        imageAdconfiguration.showEnterForeground = NO;
        
        //图片已缓存 - 显示一个 "已预载" 视图 (可选)
        //        if([XHLaunchAd checkImageInCacheWithURL:[NSURL URLWithString:ad]]){
        //            //设置要添加的自定义视图(可选)
        //            imageAdconfiguration.subViews = [self launchAdSubViews_alreadyView];
        //
        //        }
        //显示开屏广告
        [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
    }];
}

#pragma mark - Getter

-(ZYIndexDataViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYIndexDataViewModel alloc] init];
    }
    return _viewModel;
}
@end
